package com.community.controller.repair;

import com.community.entity.Repair;
import com.community.entity.RepairItem;
import com.community.entity.Result;
import com.community.service.repair.RepairService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@Controller
public class RepairController {

    @Autowired
    private RepairService repairService;


    @ResponseBody
    @PostMapping("/repair")
    public Result operateRepair(@RequestBody Repair repair){
        String id = repair.getId();

        String houseId = repair.getHouseId();
        String description = repair.getDescription();
        String mobile = repair.getMobile();
        String repairItemId = repair.getRepairItemId();
        Date appointment = repair.getAppointment();

        Map<String, String> data = new HashMap<>();
        if(id == null || id.isEmpty()) //添加
        {
            String uuid = UUID.randomUUID().toString();
            data.put("id", uuid);
            repairService.addRepair(uuid, houseId,  description, mobile, repairItemId, appointment);
        }else { //编辑
            repairService.editRepair(id, houseId, description, mobile, repairItemId,  appointment);
        }

        data.put("id", id);

        return Result.success(data);
    }

    @ResponseBody
    @GetMapping("/repair/{id}")
    public Result getRepairDetail(@PathVariable("id") String id){

        Repair repair = repairService.getDetails(id);
        Map<String, String> data = new HashMap<>();
        data.put("id", repair.getId());
        data.put("houseInfo", repair.getHouseInfo());
        data.put("houseId", repair.getHouseId());
        data.put("repairItemName", repair.getRepairItemName());
        data.put("repairItemId", repair.getRepairItemId());
        data.put("mobile", repair.getMobile());
        data.put("appointment", repair.getAppointment().toString());
        data.put("description", repair.getDescription());
        data.put("status", repair.getStatus() + "");

        return Result.success(data);
    }

    @ResponseBody
    @GetMapping("/repairItem")
    public Result getRepairItem(){

        List<RepairItem> list = repairService.getRepairItem();
        return Result.success(list);
    }


    @ResponseBody
    @PutMapping("/cancel/{id}")
    public Result cancelRepair(@PathVariable("id") String id){
        log.info("保修的id为:{}", id);
        repairService.cancelRepair(id);
        Map<String, String> data = new HashMap<>();
        data.put("id", id);
        return Result.success(data);
    }


}
